import {Login} from '@/api/user'
import $store  from '@/router/index'

export default {
    state:{
        userInfo:{},
        token:'',
    },
    mutations:{
        SET_USER_INFO(state,data){
            state.userInfo = data;
            localStorage.setItem("userInfo",JSON.stringify(data));
        },
        SET_TOKEN(state,data){
            state.token  =data;
            localStorage.setItem("token",data);
        }
    },
    actions:{
        login(context,data){
            Login(data)
            .then(res=>{
                if(res.data.code==200){
                    context.commit("SET_USER_INFO",res.data.user);
                    context.commit("SET_TOKEN",res.data.token);
                    $store.push("/admin")
                }
            })
        }
    }
}